package com.controller;

import com.dao.LeaveDao;
import com.pojo.Leave;
import com.pojo.Page;
import com.service.LeaveService;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/leave")
public class LeaveController {
    @Resource
    private LeaveService service;
    @RequestMapping("/addleave")
    @ResponseBody
    public String addleave(@RequestBody Leave leave){
        int result = service.addLeave(leave);
        if(result>0){
            return "{\"fig\":\"success\"}";
        }
        else{
            return "{\"fig\":\"error\"}";

        }
    }
    @RequestMapping("/queryAll")
    @ResponseBody
    public Page queryAll(int pageindex){
        Page page = new Page(5,pageindex);
        List<Leave> list = service.queryAll(page.getStartIndex(),page.getEndIndex());
        int count = service.countAll(null);
        page.setResult(list,count);
        return page;
    }
    @RequestMapping("/succeeleave")
    @ResponseBody
    public String successLeave(int id){
        int result =  service.successLeave(id);
        if(result>0){
            return "{\"fig\":\"success\"}";
        }
        else{
            return "{\"fig\":\"error\"}";
        }
    }
    @RequestMapping("/errorLeave")
    @ResponseBody
    public String errorLeave(int id){
        int result =  service.errorLeave(id);
        if(result>0){
            return "{\"fig\":\"success\"}";
        }
        else{
            return "{\"fig\":\"error\"}";
        }
    }
    @RequestMapping("/queryByName")
    @ResponseBody
    public Page queryByName(int pageindex,String name){
        Page page = new Page(5,pageindex);
        List<Leave> list = service.queryByName(page.getStartIndex(),page.getEndIndex(),name);
        int count=service.countAll(name);
        page.setResult(list,count);
        return page;
    }
    @RequestMapping("/querybyboth")
    @ResponseBody
    public Page queryByBoth(int pageindex,String name,int status){
        Page page = new Page(5,pageindex);
        List<Leave> list = service.queryByBoth(page.getStartIndex(),page.getEndIndex(),name,status);
        int count=service.countAll(name);
        page.setResult(list,count);
        return page;
    }
    @RequestMapping("/querystauts")
    @ResponseBody
    public int querystatus(){
        int result = service.countbystatus();
        return  result;
    }
}
